pipeline {
    agent any
    environment {

        base_url='http://139.159.214.157'

        // 测试报告
        alluredir = 'himart-test/reports/allure/allure-results'
        allure_reports = 'himart-test/reports/allure/allure-reports'
        allure_reports_url = 'localhost'
        allure_reports_url_port = '8083'

        //只测试标记为regression、function、compatibility的用例
        markers="['function','regression','compatibility']"

        // 项目地址
        project_url = 'https://gitee.com/shuwenouwan/himart-test.git'

        // Android/ios平台
        architecture = "['x86_64','arm64-v8a']"

        mobile_brand_android = "[['小米': ['xiaomi 6', 'xiaomi 10', 'xiaomi 13']],['华为': ['huawei P40', 'huawei Mate 40 Pro', 'huawei Nova 9']],['三星': ['samsung Galaxy S21', 'samsung Galaxy Note 20', 'samsung Galaxy Z Fold 3']],['oppo': ['oppo Find X3 Pro', 'oppo Reno6 Pro+', 'oppo A93']],['vivo': ['vivo X60 Pro', 'vivo Y76s', 'vivo iQOO Neo5']]]"
        platform_name = "['Android', 'ios']"
        app_url = "['himart-test/resources/app-file/himart.apk', 'himart-test/resources/app-file/himart.ipa']"
        device_name = "['device_functional_tests', 'device_integration_tests', 'device_compatibility_tests']"
        app_package_name = "['uni.UNI31459F4', 'uni.UNI31459F4']"
        app_activity_name = "['io.dcloud.PandoraEntry', 'io.dcloud.PandoraEntry']"
        automation_name = 'UiAutomator2'
        webdriver_url_appium = "${base_url}:8444/wd/hub"

        // web平台
        webdriver_url_selenium = "${base_url}:8444/wd/hub"
        browsers = "['chrome', 'edge', 'firefox']"
        headless = true

        // 小程序
    }
    stages {
        stage('构建'){
            steps {
                script {
                    sh "git clone ${project_url}"
                }
            }
        }
        //并行执行功能性测试、兼容性测试、、回归测试、、api测试、压力测试、集成测试、安全性测试
        parallel {
            stage('功能性/兼容性/回归测试') { 
                parallel { 
                    stage('功能性/兼容性/回归测试') {  
                        agent none
                        steps {
                            script {
                                def composeCommand = 'docker-compose -f himart-test/docker/docker-compose.selenium_grid.yml'
                                sh "${composeCommand} up -d selenium_grid"
                                //指定在docker-compose.selenium_grid.yml文件中命名为selenium_grid_pytest的容器中下载项目
                                sh "${composeCommand} exec selenium_grid_pytest git clone https://gitee.com/shuwenouwan/himart-test.git" 
                                //并行执行功能性、兼容性、回归测试 
                                for (marker in markers) { 
                                    //并行执行chrome、edge、Firefox等web平台的功能性测试、兼容性测试、回归测试 
                                    for (browser in browsers) { 
                                        sh """
                                           //指定在docker-compose.selenium_grid.yml文件中命名为selenium_grid_pytest的容器中执行测试代码并生成报告
                                           "${composeCommand} exec selenium_grid_pytest  pytest -m ${marker} --alluredir=${alluredir} --allure_reports=${allure_reports} --allure_reports_url=${allure_reports_url}:${allure_reports_url_port} \\
                                            --allure_reports_url_port=${allure_reports_url_port}  --webdriver_url_selenium=${webdriver_url_selenium} --browser=${browser} \\
                                            himart-test/tests/web_tests"   
                                        """
                                    }
                                }
                                sh "${composeCommand} down"
                            }
                        }
                    }
                    stage("功能性/兼容性/回归测试") {
                        agent none
                        steps {
                            script {
                                def composeCommand = 'docker-compose -f himart-test/docker/docker-compose.appium_android.yml'
                                sh "${composeCommand} up -d"
                                //指定在docker-compose.selenium_grid.yml文件中命名为appium_android_pytest的容器中下载项目
                                sh "${composeCommand} exec appium_android_pytest git clone https://gitee.com/shuwenouwan/himart-test.git"  
                                //并行执行功能性、兼容性、回归测试 
                                for (marker in markers) { 
                                    ////并行执行小米、华为、三星等手机品牌的功能性、兼容性、回归测试cc(真机测试情况下)
                                    // for (items in mobile_brand_android) { 
                                        ////"xiaomi 6", "xiaomi 10", "xiaomi 13"等小米手机型号
                                        // for (item in items) { 
                                            sh """
                                                //指定在docker-compose.selenium_grid.yml文件中命名为appium_android_pytest的容器中执行测试代码并生成报告
                                                "${composeCommand} exec appium_android_pytest pytest -m ${marker}  --alluredir=${alluredir} --allure_reports=${allure_reports} --allure_reports_url=${allure_reports_url}:${allure_reports_url_port} \\
                                                --allure_reports_url_port=${allure_reports_url_port}
                                                --platform_name=${platform_name[0]} \\ 
                                                --automation_name=${automation_name} \\
                                                --app_url=${app_url[0]} \\
                                                --device_name=${device_name[0]} \\
                                                --app_package_name=${app_package_name[0]} \\
                                                --app_activity_name=${app_activity_name[0]} \\
                                                --webdriver_url_appium=${webdriver_url_appium} \\
                                                himart-test/tests/mobile_tests"   
                                            """
                                        // }
                                    // }
                                }
                                sh "${composeCommand} down"
                            }
                        }
                    } 
                    stage("功能性/兼容性/回归测试"){
                        agent none
                        steps {
                            script {
                                sh """
                                """
                            }
                        }
                    }
                    stage("功能性/兼容性/回归测试"){
                        agent none
                        steps {
                            script {
                                sh """
                                """
                            }
                        }
                    }
                }
            }
            stage('Api接口测试') {
                agent none
                steps {
                    script {
                        def composeCommand = 'docker-compose -f himart-test/docker/docker-compose.newman.yml'
                        sh "${composeCommand} up -d" 
                        // 检查服务是否启动成功
                        def serviceCheckCommand = "${composeCommand} ps"
                        def serviceStatus = sh(script: serviceCheckCommand, returnStdout: true).trim()
                        if (serviceStatus.contains('Up')) {
                            println '服务已启动成功.'
                            //指定在docker-compose.selenium_grid.yml文件中命名为newman的容器中下载项目
                            sh "${composeCommand} exec newman git clone https://gitee.com/shuwenouwan/himart-test.git"  
                            sh "${composeCommand} exec newman newman run /tmp/postman_collection.json --reporters cli,junit"
                            // 停止服务
                            sh "${composeCommand} down"
                        } else {
                            error '服务启动失败.'
                        }
                    }
                }
            }
            stage('压力测试') { 
                agent none
                steps {
                    script { 
                        def composeCommand = 'docker-compose -f himart-test/docker/docker-compose.jmeter.yml'
                        sh "${composeCommand} up -d" 
                        // 检查服务是否启动成功
                        def serviceCheckCommand = "${composeCommand} ps"
                        def serviceStatus = sh(script: serviceCheckCommand, returnStdout: true).trim()
                        if (serviceStatus.contains('Up')) {
                            println '服务已启动成功.'
                            // 运行Newman测试
                            sh "${composeCommand} exec jmeter_container jmeter -n -t /jmx/test-plan.jmx -l /results/jmeter-results.csv --json /results/jmeter-results.json --allure /results/allure-jmeter"
                            // 停止服务
                            sh "${composeCommand} down"
                        } else {
                            error '服务启动失败.'
                        }
                    }
                }
            }   
            stage('安全性测试') {
                agent none
                steps {
                    script {
                        sh """
                        """
                    }
                }
            }  
        }
    }
}